window.onload = function() {
	//设置左右开关的值
	$(".bootstrap-switch-handle-on").eq(0).html("数据");
	$(".bootstrap-switch-handle-off").eq(0).html("3D");
}

var $activeData=new Array(10);
function Progress_bar(){
    //速度滑条(不发数据 只改动界面)
    $('#size').on('input propertychange',function(){
        data_speed_t=$('#size').val();
        $("#size").css("background-size",data_speed_t+"%"+" "+100+"%");
        $(".data_num").html(data_speed_t+"%");
    });

    // 载入点
	$("#mount_points").on('click',function(){
		$(".ipot_data").css("display","block");
		$(".ipot_data_background").css("display","block");
    });

    // 取消按钮 隐藏 点载入面板
    $("#pot_view_hide").on('click',function(){
		$(".ipot_data").css("display","none");
		$(".ipot_data_background").css("display","none");
    });

    //点击数字按键改变当前的颜色，添加类名
    $(".ipot_data>span").on('click',function(){
		$(this).css("color","#00FFFF").siblings('span').css("color","white");
		$(this).addClass("active_1").siblings('span').removeClass("active_1");
    });

    // 确定按钮 点载入面板
    $("#pot_view_record").on('click',function(){
    	var $act=$(".active_1").html();
    	$(".ipot_data>span").css("color","white");

        if(typeof($act) == "undefined"){
            log("提示:您没有选择点");
        }
        else{
            var sendPointCmd = "setPoint:"+$act+";J;";//编号 目前默认关节
        
            // 获取值
            var tmp_world = data_point_world;
            if(tmp_world.length == 6){
                for(var i=0; i<6; i++){
                    /* 新方式 */
                    sendPointCmd += tmp_world[i];
                    sendPointCmd += ";";
                }
            }

            var tmp_joint = data_point_joint;
            var tmp_joint_len = tmp_joint.length;
            for(var i=0; i<tmp_joint_len; i++){
                sendPointCmd += tmp_joint[i];
                sendPointCmd += ";";
            }
            sendPointCmd = sendPointCmd.slice(0,-1);//去掉最后一个分号
            
            console.log(sendPointCmd);

            if(workerObj_normal) {
                workerObj_normal.postMessage(sendPointCmd);//发送
            }

            // 存入全局变量
            $activeData[$act-1] = $active;

            $(".ipot_data").css("display","none");
            $(".ipot_data_background").css("display","none");
            $(".active_1").removeClass("active_1");
        }
        
    });
}
Progress_bar();

//数据与3D切换			
$(".d1").on("click", function() {
	console.log($(".d3").is('.f1'));
	if($(".d3").is('.f1')) {
		$(".d3").removeClass("f1").addClass("f2");
		$(".d2").addClass("f11").removeClass("f22");
		$("#name_1").html('点位数据');
		// if(workerObj_normal) {
		// 	workerObj_normal.postMessage("setServo:off");
		// }
	} else {
		$(".d3").removeClass("f2").addClass("f1");
		$(".d2").addClass("f22").removeClass("f11");
		$("#name_1").html('3D模型');
		// if(workerObj_normal) {
		// 	workerObj_normal.postMessage("setServo:on");
		// }
	}
});

// 修改速度
function change_speed(){
    var value = document.getElementById('size').value ;
    console.log(value)

    if(workerObj_normal){
        workerObj_normal.postMessage("setSpeed:"+value+";T");
    } 

    var setA = setTimeout(function(){
        // console.log(1);
        var tmp = value/2;
        if(workerObj_normal){
            workerObj_normal.postMessage("setSpeed:"+tmp.toFixed(0)+";A");
        } 
    },1000);
}

// 修改坐标系
function coordChange()
{
    var obj = document.getElementById("lnch_operation");
    var changeVal = obj.options[obj.selectedIndex].value;
    // console.log(changeVal);
    if(workerObj_normal) {
        workerObj_normal.postMessage("setCoord:" + changeVal);
    }
}

$("#removeSpeed").on('click',function(){//速度单击-
	con--;
	$(".data_num").html(con+"%");
	$("#size").css("background-size",con+"%"+" "+100+"%");
	$('#size')[0].value=con;

	//通信
    if(workerObj_normal){
       workerObj_normal.postMessage("setSpeed:"+con+";T");
    }

    var setA = setTimeout(function(){
        // console.log(1);
        var tmp = con/2;
        if(workerObj_normal){
            workerObj_normal.postMessage("setSpeed:"+tmp.toFixed(0)+";A");
        } 
    },1000);
});
$("#addSpeed").on('click',function(){//速度单击+
	con++;
	$(".data_num").html(con+"%");
	$("#size").css("background-size",con+"%"+" "+100+"%");
	$('#size')[0].value=con;

	//通信
    if(workerObj_normal){
	   workerObj_normal.postMessage("setSpeed:"+con+";T");
    }

    var setA = setTimeout(function(){
        // console.log(1);
        var tmp = con/2;
        if(workerObj_normal){
            workerObj_normal.postMessage("setSpeed:"+tmp.toFixed(0)+";A");
        } 
    },1000);
});

//寸动按钮
//寸动按钮初始化
function initJog(){
    var jogTimeout;//time out object
    var jogTimeoutFlag = false;//用于判断定时器是否开启
    var jogNum;
    var jogDog = false;

    for(var i = 0;i<7;i++){
        var num = i+1;
        
        var l_jogname = "#jog_l"+num;//dom object
        var r_jogname = "#jog_r"+num;

        $(l_jogname).on("touchstart",function(ev){
            window.event? window.event.returnValue = false : ev.preventDefault();//解决长按右键

            if(data_model == "T1" && data_runstate == "stop")
            {
                jogNum = this.id.substring(5,6);//获取id的数字
                
                if(!jogTimeoutFlag)//若没有开启
                {
                    jogTimeoutFlag = true; //标志有效

                    jogTimeout=setInterval(function(){
                        if (workerObj_normal) {
                            if(jogDog){
                                //ws.send("jogDog__:");
                                workerObj_normal.postMessage("jogDog__:");
                            }
                            else{
                                workerObj_normal.postMessage("jogStart:"+jogNum+";L");
                                //ws.send("jogStart:"+jogNum+";L");
                                jogDog = true;
                            }
                        }
                    },200);
                }
            }
        });

        $(l_jogname).on("touchend",function(){
            if(jogTimeoutFlag) //若有效
            {
                clearInterval(jogTimeout);
                jogTimeout = null;

                if (workerObj_normal) {
                    workerObj_normal.postMessage("stop____:");
                    jogDog = false;
                }

                jogTimeoutFlag = false;//标志无效
            }
        });

        $(r_jogname).on("touchstart",function(ev){
            window.event? window.event.returnValue = false : ev.preventDefault();//解决长按右键

            if(data_model == "T1" && data_runstate == "stop")
            {
                jogNum = this.id.substring(5,6);//获取id的数字
                if(!jogTimeoutFlag)//若没有开启
                {
                    jogTimeoutFlag = true; //标志有效

                    jogTimeout=setInterval(function(){
                        if (workerObj_normal) {
                            if(jogDog){
                                workerObj_normal.postMessage("jogDog__:");
                            }
                            else{
                                workerObj_normal.postMessage("jogStart:"+jogNum+";R");
                                jogDog = true;
                            }
                        }
                    },200);
                }
            }
            
        });

        $(r_jogname).on("touchend",function(){
            if(jogTimeoutFlag) //若有效
            {
                clearInterval(jogTimeout);
                jogTimeout = null;

                if (workerObj_normal) {
                    workerObj_normal.postMessage("stop____:");
                    jogDog = false;
                }

                jogTimeoutFlag = false;//标志无效
            }
        });
    }
}
initJog();//执行
